class Solution01 {

    public List<Integer> rightSideView(TreeNode root) {
		Queue<TreeNode> queue = new LinkedList<>();
		List<Integer> results = new ArrayList<>();

		if(root != null){
			queue.offer(root);
		}
		while(!queue.isEmpty()){
			int len = queue.size();
			for(int i = 0; i < len; i++){
				TreeNode curNode = queue.poll();
				//该层最右边节点
				if(i == len - 1){
					results.add(curNode.val);
				}
				if(curNode.left != null){
					queue.offer(curNode.left);
				}
				if(curNode.right != null){
					queue.offer(curNode.right);
				}
			}
		}

		return results;
    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

